package com.bjsxt.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bjsxt.commons.DataGridModel;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UserService;


/**
 * 
 * @author clw
 *
 */

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	@RequestMapping("/addUser")
	@ResponseBody
	public Map<String, Integer> addUser(Users user){
		Map<String, Integer> map = new HashMap<>();
		try{
			this.userService.addUser(user);
			map.put("state", 200);
			return map;
		}catch(Exception e){
			map.put("state", 500);
			return map;
		}
	}
	
	@RequestMapping("showUsers")
	@ResponseBody
	public DataGridModel showUsers(Integer page,Integer rows){
		return this.userService.showUsers(page, rows);
	}
	
	@RequestMapping("delUser")
	@ResponseBody
	public Map<String, Integer> delUser(Integer userid){
		Map<String, Integer> map = new HashMap<String, Integer>();
		try{
			this.userService.dropUser(userid);
			map.put("state", 200);
			return map;
		}catch(Exception e){
			e.printStackTrace();
			map.put("state", 500);
			return map;
		}
	}
	
	@RequestMapping("updateUser")
	@ResponseBody
	public Map<String, Integer> updateUser(Users user){
		Map<String,Integer> map = new HashMap<>();
		try{
			this.userService.updateUser(user);
			map.put("state", 200);
		}catch(Exception e){
			e.printStackTrace();
			map.put("state", 500);
		}
		return map;
	}
}
